package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskWithdrawBlackWhiteListMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.RiskWithdrawBlackWhiteListDO;
import com.sztx.pay.center.rpc.api.domain.PageModelResponse;
import com.sztx.pay.center.rpc.api.domain.WithdrawAuthListRequest;
import com.sztx.pay.center.rpc.api.domain.WithdrawAuthListResponse;
import com.sztx.pay.center.rpc.api.service.WithdrawAuthRpcService;
import com.sztx.se.core.service.impl.BaseServiceImpl;
@Service
public class WithdrawAuthRpcServiceImpl extends BaseServiceImpl implements WithdrawAuthRpcService{
	@Autowired
	RiskWithdrawBlackWhiteListMysqlDAO riskWithDrawAuthDao;
	
	@Override
	public void setWithDrawAuth(WithdrawAuthListRequest withdrawAuthListRequest) {
		ValidateUtil.isNotBlank(withdrawAuthListRequest.getUserId(),"倍棒帐号不能为空");
		ValidateUtil.isBetween(withdrawAuthListRequest.getStatus(), 0, 1, "状态提交错误");
		ValidateUtil.isBetween(withdrawAuthListRequest.getType(),1, 2, "类型提交错误");
		
		RiskWithdrawBlackWhiteListDO riskWithdrawBlackWhiteListDO = new RiskWithdrawBlackWhiteListDO();
		riskWithdrawBlackWhiteListDO.setUserId(withdrawAuthListRequest.getUserId());
		riskWithdrawBlackWhiteListDO.setRealName(withdrawAuthListRequest.getRealName());
		riskWithdrawBlackWhiteListDO.setType(withdrawAuthListRequest.getType());
		riskWithdrawBlackWhiteListDO.setStatus(withdrawAuthListRequest.getStatus());
		riskWithdrawBlackWhiteListDO.setOperator(withdrawAuthListRequest.getOperator());
		riskWithdrawBlackWhiteListDO.setChannelId(withdrawAuthListRequest.getChannelId());
		
		
		RiskWithdrawBlackWhiteListDO withDrawAuthDo = riskWithDrawAuthDao.findByUser(withdrawAuthListRequest.getUserId());
		if(withDrawAuthDo != null){
			riskWithDrawAuthDao.update(riskWithdrawBlackWhiteListDO);
		}else{
			riskWithDrawAuthDao.save(riskWithdrawBlackWhiteListDO);
		}
	}

	@Override
	@SlaveQueryAnnotation
	public PageModelResponse<WithdrawAuthListResponse> getWithDrawAuthList(
			WithdrawAuthListRequest withdrawAuthListRequest, int page,
			int pagesize) {
		RiskWithdrawBlackWhiteListDO riskWithdrawBlackWhiteListDO = new RiskWithdrawBlackWhiteListDO();
		riskWithdrawBlackWhiteListDO.setUserId(withdrawAuthListRequest.getUserId());
		riskWithdrawBlackWhiteListDO.setRealName(withdrawAuthListRequest.getRealName());
		riskWithdrawBlackWhiteListDO.setType(withdrawAuthListRequest.getType());
		riskWithdrawBlackWhiteListDO.setStatus(withdrawAuthListRequest.getStatus());
		riskWithdrawBlackWhiteListDO.setOperator(withdrawAuthListRequest.getOperator());
		riskWithdrawBlackWhiteListDO.setChannelId(withdrawAuthListRequest.getChannelId());
		
		Integer total = riskWithDrawAuthDao.listCount(riskWithdrawBlackWhiteListDO);
		int totalPage = (total % pagesize >0) ? (total / pagesize + 1) : total / pagesize;
		int index = (page - 1) * pagesize;
		
		List<RiskWithdrawBlackWhiteListDO> authlist = riskWithDrawAuthDao.findauthlist(riskWithdrawBlackWhiteListDO, index, pagesize);
		List<WithdrawAuthListResponse> list = new ArrayList<WithdrawAuthListResponse>();
		for (int i = 0; i < authlist.size(); i++) {
			RiskWithdrawBlackWhiteListDO withDrawAuthDo = authlist.get(i);
			WithdrawAuthListResponse response = new WithdrawAuthListResponse();
			response.setUserId(withDrawAuthDo.getUserId());
			response.setType(withDrawAuthDo.getType());
			response.setRealName(withDrawAuthDo.getRealName());
			response.setStatus(withDrawAuthDo.getStatus());
			list.add(response);
		}
		
		PageModelResponse<WithdrawAuthListResponse> pageList = new PageModelResponse<WithdrawAuthListResponse>();
		pageList.setPageNo(page);
		pageList.setPageSize(pagesize);
		pageList.setTotal(total);
		pageList.setTotalPage(totalPage);
		pageList.setList(list);
		return pageList;
	}

	@Override
	@SlaveQueryAnnotation
	public WithdrawAuthListResponse getWithDrawAuthByUserId(String userId) {
		ValidateUtil.isNotBlank(userId,"userId不能为空");
		return getWithDrawAuthByUserId(userId, "10");
	}

	@Override
	@SlaveQueryAnnotation
	public WithdrawAuthListResponse getWithDrawAuthByUserId(String userId,String channelId) {
		ValidateUtil.isNotBlank(userId,"userId不能为空");
		ValidateUtil.isNotBlank(channelId,"channelId不能为空");
		WithdrawAuthListResponse response = null;
		
		
		//非缴费渠道、wbank渠道直接放过
		if(!channelId.equals("99") && !channelId.equals("90")){
			response = new WithdrawAuthListResponse();
			response.setUserId(userId);
			response.setType(new Byte("1"));
			response.setRealName("");
			response.setStatus(new Byte("1"));
			return response;
		}
		
		
		
		
		
		RiskWithdrawBlackWhiteListDO withDrawAuthDo = riskWithDrawAuthDao.findByUserAndChannelId(userId, channelId);
		if(withDrawAuthDo != null){
			response = new WithdrawAuthListResponse();
			response.setUserId(withDrawAuthDo.getUserId());
			response.setType(withDrawAuthDo.getType());
			response.setRealName(withDrawAuthDo.getRealName());
			response.setStatus(withDrawAuthDo.getStatus());
		}
		return response;
	}
	

}
